package com.changgou.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class DelayConfig {

    @Autowired
    private Environment environment;

    //创建交换机
    @Bean
    public Exchange exchange() {
        return new DirectExchange(environment.getProperty("mq.delay.exchange.order"), true, false);
    }

    //创建队列(普通支付)
    @Bean
    public Queue msgQueue() {
        return new Queue(environment.getProperty("mq.delay.queue.order"), true, false, false);
    }

    //创建延时队列(普通支付)
    @Bean
    public Queue delayQueue() {
        return QueueBuilder.durable(environment.getProperty("mq.delay.delayQueue.order"))
                .withArgument("x-dead-letter-exchange", environment.getProperty("mq.delay.exchange.order")) //消息超时进入死信队列，绑定死信队列交换机
                .withArgument("x-dead-letter-routing-key", environment.getProperty("mq.delay.queue.order")) //绑定指定路由，路由与消息发出队列名相同
                .build();
    }

    //绑定队列与交换机(普通支付)
    @Bean
    public Binding binding(Exchange exchange, Queue msgQueue) {
        return BindingBuilder.bind(msgQueue).to(exchange).with(environment.getProperty("mq.delay.queue.order")).noargs();
    }

    //创建队列(秒杀支付)
    @Bean
    public Queue seckillQueue() {
        return new Queue(environment.getProperty("mq.delay.queue.seckillorder"), true, false, false);
    }

    //创建延时队列(秒杀支付)
    @Bean
    public Queue delaySeckillQueue() {
        return QueueBuilder.durable(environment.getProperty("mq.delay.delayQueue.seckillorder"))
                .withArgument("x-dead-letter-exchange", environment.getProperty("mq.delay.exchange.order")) //消息超时进入死信队列，绑定死信队列交换机
                .withArgument("x-dead-letter-routing-key", environment.getProperty("mq.delay.queue.seckillorder")) //绑定指定路由，路由与消息发出队列名相同
                .build();
    }

    //绑定队列与交换机(秒杀支付)
    @Bean
    public Binding seckillBinding(Exchange exchange, Queue seckillQueue) {
        return BindingBuilder.bind(seckillQueue).to(exchange).with(environment.getProperty("mq.delay.queue.seckillorder")).noargs();
    }

}
